﻿using System.Collections.Generic;
using System.Threading.Tasks;
using B.MESUI.Login.A_Public.Constants;
using B.MESUI.Login.A_Public.Helpers;
using B.MESUI.Login.A_Public.Model;
using B.MESUI.Login.ProdManagement.DTO;
using B.MESUI.Login.ProdManagement.Model;
using Newtonsoft.Json;

namespace B.MESUI.Login.ProdManagement.Services
{
    /// <summary>
    /// 产品锁定服务类
    /// </summary>
    public static class ProductLockService
    {
        /// <summary>
        /// 添加产品锁定。
        /// </summary>
        /// <param name="request">产品锁定请求对象</param>
        /// <returns>操作是否成功</returns>
        public static async Task<bool> AddProductLockAsync(AddProductLockRequest request)
        {
            try
            {
                var url = HttpClientHelper.GetProductLockUrl(ApiEndpoints.ProductLock.AddProductLock);
                var response = await HttpClientHelper.PostAsync(url, request);
                var result = JsonConvert.DeserializeObject<ApiResponse<object>>(response);
                return result.Success == true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取产品锁定列表。
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns>分页的产品锁定数据</returns>
        public static async Task<PagedResponse<ProductLockDto>> GetProductLockListAsync(int pageIndex = 1, int pageSize = 10)
        {
            try
            {
                var url = HttpClientHelper.GetProductLockUrl(ApiEndpoints.ProductLock.GetProductLockList);
                var parameters = new Dictionary<string, object>
                {
                    { "PageIndex", pageIndex },
                    { "PageSize", pageSize }
                };

                var queryString = HttpClientHelper.BuildQueryString(parameters);
                url += $"?{queryString}";

                var response = await HttpClientHelper.GetAsync(url);
                return JsonConvert.DeserializeObject<PagedResponse<ProductLockDto>>(response);
            }
            catch
            {
                return new PagedResponse<ProductLockDto> { pageDatas = new List<ProductLockDto>() };
            }
        }

        /// <summary>
        /// 解锁产品。
        /// </summary>
        /// <param name="productIds">要解锁的产品ID列表</param>
        /// <returns>操作是否成功</returns>
        public static async Task<bool> UnlockProductsAsync(List<string> productIds)
        {
            try
            {
                var url = HttpClientHelper.GetProductLockUrl(ApiEndpoints.ProductLock.UnProductLock);
                var request = new UnlockProductRequest { productiDs = productIds };
                var response = await HttpClientHelper.PutAsync(url, request);
                var result = JsonConvert.DeserializeObject<ApiResponse<object>>(response);
                return result?.Success ?? false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取工单实体（用于产品锁定）。
        /// </summary>
        /// <param name="codeId">工单编码</param>
        /// <param name="workOrderType">工单类型</param>
        /// <returns>工单实体对象</returns>
        public static async Task<WorkOrderListItem> GetWorkOrderEntityAsync(string codeId, string workOrderType)
        {
            try
            {
                var url = HttpClientHelper.GetProductLockUrl(ApiEndpoints.ProductLock.GetWorkOrderEntity);
                var parameters = new Dictionary<string, object>
                {
                    { "CODEID", codeId },
                    { "WORKORDER_TYPE", workOrderType }
                };

                var queryString = HttpClientHelper.BuildQueryString(parameters);
                url += $"?{queryString}";

                var response = await HttpClientHelper.GetAsync(url);
                var result = JsonConvert.DeserializeObject<ApiResponse<WorkOrderListItem>>(response);
                return result?.resultData;
            }
            catch
            {
                return null;
            }
        }
    }
}